package Utils

import (
	"bytes"
	"crypto/aes"
	"crypto/cipher"
)

type ecb struct {
	b         cipher.Block
	blockSize int
}

func newECB(b cipher.Block) *ecb {
	return &ecb{
		b:         b,
		blockSize: b.BlockSize(),
	}
}

type ecbEncrypter ecb

func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
	return (*ecbEncrypter)(newECB(b))
}
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
	if len(src)%x.blockSize != 0 {
		panic("crypto/cipher: input not full blocks")
	}
	if len(dst) < len(src) {
		panic("crypto/cipher: output smaller than input")
	}
	for len(src) > 0 {
		x.b.Encrypt(dst, src[:x.blockSize])
		src = src[x.blockSize:]
		dst = dst[x.blockSize:]
	}
}

type ecbDecrypter ecb

// NewECBDecrypter returns a BlockMode which decrypts in electronic code book
// mode, using the given Block.
func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
	return (*ecbDecrypter)(newECB(b))
}
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
	if len(src)%x.blockSize != 0 {
		panic("crypto/cipher: input not full blocks")
	}
	if len(dst) < len(src) {
		panic("crypto/cipher: output smaller than input")
	}
	for len(src) > 0 {
		x.b.Decrypt(dst, src[:x.blockSize])
		src = src[x.blockSize:]
		dst = dst[x.blockSize:]
	}
}

type Mode int

const (
	ECB Mode = iota
	CBC
)

type PadMode int

const (
	PKCS7Pad PadMode = iota
	ZeroPad
	PKCS5Pad
	NoPad
)


func switchPadMode(origData []byte, blockSize int, padMode PadMode) []byte {
	var newData []byte
	switch padMode {
	case PKCS7Pad:
		newData = PKCS7Padding(origData, blockSize)
	case PKCS5Pad:
		newData = PKCS5Padding(origData, blockSize)
	case ZeroPad:
		newData = ZeroPadding(origData, blockSize)
	default:
		newData = origData
	}
	return newData
}

func switchUnPadMode(origData []byte, blockSize int, padMode PadMode) []byte {
	var newData []byte
	switch padMode {
	case PKCS7Pad:
		newData = PKCS7UnPadding(origData)
	case PKCS5Pad:
		newData = PKCS5UnPadding(origData)
	case ZeroPad:
		newData = ZeroUnPadding(origData)
	default:
		newData = origData
	}
	return newData
}

func encode(origData, key, vi []byte, mode Mode, padMode PadMode) ([]byte, error) {
	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}

	blockSize := block.BlockSize()
	newData := switchPadMode(origData, blockSize, padMode)

	var blockMode cipher.BlockMode
	switch mode {
	case ECB:
		blockMode = NewECBEncrypter(block)
	case CBC:
		blockMode = cipher.NewCBCEncrypter(block, vi[:blockSize])

	default:

	}
	ciphertext := make([]byte, len(newData))
	blockMode.CryptBlocks(ciphertext, newData)
	return ciphertext, nil
}

func decode(origData, key, vi []byte, mode Mode, padMode PadMode) ([]byte, error) {
	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}
	blockSize := block.BlockSize()
	var blockMode cipher.BlockMode
	switch mode {
	case ECB:

		blockMode = NewECBDecrypter(block)
	case CBC:
		blockMode = cipher.NewCBCDecrypter(block, vi[:blockSize])

	default:

	}
	newData := make([]byte, len(origData))
	blockMode.CryptBlocks(newData, origData)
	newData = switchUnPadMode(newData, blockSize, padMode)
	return newData, nil
}

func ZeroPadding(ciphertext []byte, blockSize int) []byte {
	padding := blockSize - len(ciphertext)%blockSize
	padtext := bytes.Repeat([]byte{0}, padding)
	return append(ciphertext, padtext...)
}

func ZeroUnPadding(origData []byte) []byte {
	length := len(origData)
	unpadding := int(origData[length-1])
	return origData[:(length - unpadding)]
}

func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
	return PKCS7Padding(ciphertext, 8)
}

func PKCS5UnPadding(origData []byte) []byte {
	length := len(origData)
	// 去掉最后一个字节 unpadding 次
	unpadding := int(origData[length-1])
	return origData[:(length - unpadding)]
}

func PKCS7Padding(ciphertext []byte, blockSize int) []byte {
	padding := blockSize - len(ciphertext)%blockSize // 需要填充的数目

	padtext := bytes.Repeat([]byte{byte(padding)}, padding) // 生成填充的文本

	return append(ciphertext, padtext...)

}

func PKCS7UnPadding(origData []byte) []byte {
	length := len(origData)

	unpadding := int(origData[length-1])

	return origData[:(length - unpadding)]

}


func AesEncryptECB(origData, key []byte, padMode PadMode) ([]byte, error) {
	return encode(origData, key, nil, ECB, padMode)
}

func AesDecryptECB(origData, key []byte, padMode PadMode) ([]byte, error) {
	return decode(origData, key, nil, ECB, padMode)
}

func AesEncryptCBC(origData, key, vi []byte, padMode PadMode) ([]byte, error) {
	return encode(origData, key, vi, CBC, padMode)
}

func AesDecryptCBC(origData, key, vi []byte, padMode PadMode) ([]byte, error) {
	return decode(origData, key, vi, CBC, padMode)
}
